<template xmlns:background-color="http://www.w3.org/1999/xhtml">
  <a-row :gutter="10">
    <a-col :md="6" :sm="20">
      <a-card :bordered="false">
        <!-- 按钮操作区域 -->
        <a-row style="margin-left: 14px">
          <!--<a-button @click="refresh" type="default" icon="reload" :loading="loading">刷新</a-button>-->
        </a-row>
        <div style="background: #fff;padding-left:16px;height: 100%; margin-top: 5px">
          <a-alert type="info" :showIcon="true">
            <div slot="message">
              当前选择：<span v-if="this.currSelected.title">{{ getCurrSelectedTitle() }}</span>
            </div>
          </a-alert>
          <a-input-search @search="onSearch" style="width:100%;margin-top: 10px" placeholder="请输入部门名称" />
          <!-- 树-->
          <a-col :md="10" :sm="24">
            <template>
              <span style="user-select: none">
                <a-tree
                  @select="onSelect"
                  :selectedKeys="selectedKeys"
                  :treeData="departTree"
                  :checkStrictly="checkStrictly"
                />
              </span>
            </template>
          </a-col>
        </div>
      </a-card>
      <!---- author:os_chengtgen -- date:20190827 --  for:切换父子勾选模式 =======------>
<!--      <div class="drawer-bootom-button">-->
<!--        <a-dropdown :trigger="['click']" placement="topCenter">-->
<!--          <a-menu slot="overlay">-->
<!--            &lt;!&ndash; <a-menu-item key="1" @click="switchCheckStrictly(1)">父子关联</a-menu-item>-->
<!--            <a-menu-item key="2" @click="switchCheckStrictly(2)">取消关联</a-menu-item> &ndash;&gt;-->
<!--            <a-menu-item key="5" @click="expandAll">展开所有</a-menu-item>-->
<!--            <a-menu-item key="6" @click="closeAll">合并所有</a-menu-item>-->
<!--          </a-menu>-->
<!--          <a-button> 树操作 <a-icon type="up" /> </a-button>-->
<!--        </a-dropdown>-->
<!--      </div>-->
      <!---- author:os_chengtgen -- date:20190827 --  for:切换父子勾选模式 =======------>
    </a-col>
    <a-col :md="18" :sm="24">
      <div style="width: 100%;background: #FFFFFF;padding: 15px;">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :md="6" :sm="12">
              <a-form-item label="账号">
                <!--<a-input placeholder="请输入账号查询" v-model="queryParam.username"></a-input>-->
                <j-input placeholder="输入账号模糊查询" v-model="queryParam.username"></j-input>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
            <span style="float: left;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
            </a-col>
          </a-row>
        </a-form>
        <div style="margin-top: 20px;">
          <a-table
            ref="table"
            size="middle"
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
            :loading="loading">
          </a-table>
        </div>
      </div>
    </a-col>
  </a-row>
</template>
<script>
import { queryDepartTreeList, searchByKeywords } from '@/api/api'
import { httpAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import Vue from 'vue'
// 表头

export default {
  name: 'DepartList',
  mixins: [JeecgListMixin],
  components: {},
  data() {
    return {
      iExpandedKeys: [],
      loading: false,
      autoExpandParent: false,
      currFlowId: '',
      currFlowName: '',
      disable: true,
      treeData: [],
      visible: false,
      departTree: [],
      hiding: true,
      model: {},
      depart: {},
      disableSubmit: false,
      selectedKeys: [],
      autoIncr: 1,
      currSelected: {},
      allTreeKeys: [],
      checkStrictly: true,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      graphDatasource: {
        nodes: [],
        edges: []
      },
      orgCategoryDisabled: false,
      oldDirectorUserIds: '',
      queryParam: {},
      columns: [
        {
          title: '用户账号',
          align: "center",
          dataIndex: 'username',
          width: 120,
          sorter: true
        },
        {
          title: '用户姓名',
          align: "center",
          width: 100,
          dataIndex: 'realname',
        },
        // {
        //   title: '头像',
        //   align: "center",
        //   width: 120,
        //   dataIndex: 'avatar',
        //   scopedSlots: {customRender: "avatarslot"}
        // },
        {
          title: '性别',
          align: "center",
          width: 80,
          dataIndex: 'sex_dictText',
          sorter: true
        },
        {
          title: '生日',
          align: "center",
          width: 100,
          dataIndex: 'birthday'
        },
        {
          title: '手机号码',
          align: "center",
          width: 100,
          dataIndex: 'phone'
        },
        {
          title: '部门',
          align: "center",
          width: 180,
          dataIndex: 'orgCodeTxt'
        },
        {
          title: '负责部门',
          align: "center",
          width: 180,
          dataIndex: 'departIds_dictText'
        },
        {
          title: '状态',
          align: "center",
          width: 80,
          dataIndex: 'status_dictText'
        },
        // {
        //   title: '操作',
        //   dataIndex: 'action',
        //   scopedSlots: {customRender: 'action'},
        //   align: "center",
        //   width: 170
        // }
      ],
      url: {
        syncUser: "/act/process/extActProcess/doSyncUser",
        list: "/sys/user/list",
        delete: "/sys/user/delete",
        deleteBatch: "/sys/user/deleteBatch",
        exportXlsUrl: "/sys/user/exportXls",
        importExcelUrl: "sys/user/importExcel",
      },
    }
  },
  computed: {

  },
  mounted() {
    this.refresh()
  },
  methods: {
    // loadData() {
    //   this.refresh()
    // },
    loadTree() {
      var that = this
      that.treeData = []
      that.departTree = []
      queryDepartTreeList().then(res => {
        if (res.success) {
          //部门全选后，再添加部门，选中数量增多
          this.allTreeKeys = []
          for (let i = 0; i < res.result.length; i++) {
            let temp = res.result[i]
            that.treeData.push(temp)
            that.departTree.push(temp)
            that.setThisExpandedKeys(temp)
            that.getAllKeys(temp)
            // console.log(temp.id)
          }
          this.loading = false
        }
      })
    },
    setThisExpandedKeys(node) {
      if (node.children && node.children.length > 0) {
        this.iExpandedKeys.push(node.key)
        for (let a = 0; a < node.children.length; a++) {
          this.setThisExpandedKeys(node.children[a])
        }
      }
    },
    refresh() {
      this.loading = true
      this.loadTree()
    },
    onExpand(expandedKeys) {
      console.log('onExpand', expandedKeys)
      this.iExpandedKeys = expandedKeys
      this.autoExpandParent = false
    },
    backFlowList() {
      this.$router.back(-1)
    },

    addRootNode() {
      this.$refs.nodeModal.add(this.currFlowId, '')
    },
    onSearch(value) {
      let that = this
      if (value) {
        searchByKeywords({ keyWord: value }).then(res => {
          if (res.success) {
            that.departTree = []
            for (let i = 0; i < res.result.length; i++) {
              let temp = res.result[i]
              that.departTree.push(temp)
            }
          } else {
            that.$message.warning(res.message)
          }
        })
      } else {
        that.loadTree()
      }
    },
    nodeModalOk() {
      this.loadTree()
    },
    nodeModalClose() {},
    hide() {
      console.log(111)
      this.visible = false
    },
    onSelect(selectedKeys, e) {
      console.log('selected', selectedKeys, e)
      this.hiding = false
      let record = e.node.dataRef
      console.log('onSelect-record', record)
      this.currSelected = Object.assign({}, record)
      this.model = this.currSelected
      this.selectedKeys = [record.key]
      this.model.parentId = record.parentId
      this.setValuesToForm(record)
      this.oldDirectorUserIds = record.directorUserIds

      this.queryParam.departids = selectedKeys[0]
      this.loadData()
    },
    // 触发onSelect事件时,为部门树右侧的form表单赋值
    setValuesToForm(record) {
      if (record.orgCategory == '1') {
        this.orgCategoryDisabled = true
      } else {
        this.orgCategoryDisabled = false
      }
    },
    getCurrSelectedTitle() {
      return !this.currSelected.title ? '' : this.currSelected.title
    },
    handleNodeTypeChange(val) {
      this.currSelected.nodeType = val
    },
    notifyTriggerTypeChange(value) {
      this.currSelected.notifyTriggerType = value
    },
    receiptTriggerTypeChange(value) {
      this.currSelected.receiptTriggerType = value
    },
    submitCurrForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (!this.currSelected.id) {
            this.$message.warning('请点击选择要修改部门!')
            return
          }

          //update-begin---author:wangshuai ---date:20200308  for：[JTC-119]在部门管理菜单下设置部门负责人
          this.currSelected.oldDirectorUserIds = this.oldDirectorUserIds
          //update-end---author:wangshuai ---date:20200308  for：[JTC-119]在部门管理菜单下设置部门负责人

          httpAction(this.url.edit, this.currSelected, 'put').then(res => {
            if (res.success) {
              this.$message.success('保存成功!')
              this.loadTree()
            } else {
              this.$message.error(res.message)
            }
          })
        }
      })
    },
    emptyCurrForm() {
      this.$refs.form.resetFields()
      this.model = {}
    },
    nodeSettingFormSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          console.log('Received values of form: ', this.model)
        }
      })
    },
    openSelect() {
      this.$refs.sysDirectiveModal.show()
    },
    selectDirectiveOk(record) {
      console.log('选中指令数据', record)
      this.nodeSettingForm.setFieldsValue({ directiveCode: record.directiveCode })
      this.currSelected.sysCode = record.sysCode
    },
    getFlowGraphData(node) {
      this.graphDatasource.nodes.push({
        id: node.id,
        text: node.flowNodeName
      })
      if (node.children.length > 0) {
        for (let a = 0; a < node.children.length; a++) {
          let temp = node.children[a]
          this.graphDatasource.edges.push({
            source: node.id,
            target: temp.id
          })
          this.getFlowGraphData(temp)
        }
      }
    },
    //---- author:os_chengtgen -- date:20190827 --  for:切换父子勾选模式 =======------
    expandAll() {
      console.log('展开所有')
      this.iExpandedKeys = this.allTreeKeys
    },
    closeAll() {
      console.log('闭合所有')
      this.iExpandedKeys = []
    },
    // switchCheckStrictly(v) {
    //   if (v == 1) {
    //     this.checkStrictly = false
    //   } else if (v == 2) {
    //     this.checkStrictly = true
    //   }
    // },
    getAllKeys(node) {
      // console.log('node',node);
      this.allTreeKeys.push(node.key)
      if (node.children && node.children.length > 0) {
        for (let a = 0; a < node.children.length; a++) {
          this.getAllKeys(node.children[a])
        }
      }
    }
    //---- author:os_chengtgen -- date:20190827 --  for:切换父子勾选模式 =======------
  },
  created() {
    this.currFlowId = this.$route.params.id
    this.currFlowName = this.$route.params.name
    // this.loadTree()
  }
}
</script>
<style scoped>
.ant-card-body .table-operator {
  margin: 15px;
}

.anty-form-btn {
  width: 100%;
  text-align: center;
}

.anty-form-btn button {
  margin: 0 5px;
}

.anty-node-layout .ant-layout-header {
  padding-right: 0;
}

.header {
  padding: 0 8px;
}

.header button {
  margin: 0 3px;
}

.ant-modal-cust-warp {
  height: 100%;
}

.ant-modal-cust-warp .ant-modal-body {
  height: calc(100% - 110px) !important;
  overflow-y: auto;
}

.ant-modal-cust-warp .ant-modal-content {
  height: 90% !important;
  overflow-y: hidden;
}

#app .desktop {
  height: auto !important;
}

/** Button按钮间距 */
.ant-btn {
  margin-left: 3px;
}

.drawer-bootom-button {
  /*position: absolute;*/
  bottom: 0;
  width: 100%;
  border-top: 1px solid #e8e8e8;
  padding: 10px 16px;
  text-align: left;
  left: 0;
  background: #fff;
  border-radius: 0 0 2px 2px;
}
</style>
